package com.sjm.suchbear.minibookmall.manage;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.BookDB;
import com.sjm.suchbear.minibookmall.R;
import com.sjm.suchbear.minibookmall.Utils.T;
import com.sjm.suchbear.minibookmall.action.BookManage;
import com.sjm.suchbear.minibookmall.model.BookModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddBookActivity extends Activity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.et_bookname)
    EditText etBookname;
    @BindView(R.id.et_author)
    EditText etAuthor;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.et_price)
    EditText etPrice;
    @BindView(R.id.tv_addBook)
    TextView tvAddBook;
    @BindView(R.id.tv_layoutTitle)
    TextView tvLayoutTitle;

    private String bookName;
    private String bookAuth;
    private String bookContent;
    private String bookPrice;
    private SQLiteDatabase dbread;
    private int mode; //默认0:新增 1修改
    private BookModel bookModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_book);
        ButterKnife.bind(this);
        mode = getIntent().getIntExtra("mode", 0);
        if (mode == 1) {
            bookModel = (BookModel) getIntent().getExtras().getSerializable("bookModel");
        }
        BookDB db = new BookDB(getBaseContext());
        dbread = db.getReadableDatabase();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mode == 0) {
            tvListTitle.setText("新增书籍");
            tvAddBook.setText("新增书籍");
            tvLayoutTitle.setText("新增书籍");
        } else {
            tvListTitle.setText("修改书籍信息");
            tvAddBook.setText("修改书籍信息");
            tvLayoutTitle.setText("修改书籍信息");
        }

        if (mode == 1) {
            etBookname.setText(bookModel.getBookname());
            etAuthor.setText(bookModel.getBookauth());
            etContent.setText(bookModel.getBookcontent());
            etPrice.setText(bookModel.getBookprice());
        }
    }

    @OnClick({R.id.ll_header_back, R.id.tv_addBook})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_header_back:
                finish();
                break;
            case R.id.tv_addBook:
                if (mode == 0) {
                    addBook();
                } else if (mode == 1) {
                    editBook();
                }
                break;
        }
    }

    private void addBook() {
        bookName = etBookname.getText().toString();
        bookAuth = etAuthor.getText().toString();
        bookContent = etContent.getText().toString();
        bookPrice = etPrice.getText().toString();
        if (bookName.equals("")) {
            T.showShort(getBaseContext(), "请输入书名");
            return;
        }
        if (bookAuth.equals("")) {
            T.showShort(getBaseContext(), "请输入作者");
            return;
        }
        if (bookContent.equals("")) {
            T.showShort(getBaseContext(), "请输入介绍");
            return;
        }
        if (bookPrice.equals("")) {
            T.showShort(getBaseContext(), "请输入定价");
            return;
        }
        BookManage.addBook(dbread, bookName, bookAuth, bookContent, bookPrice);
        T.showShort(getBaseContext(), "添加成功");
        finish();
    }

    private void editBook() {
        bookName = etBookname.getText().toString();
        bookAuth = etAuthor.getText().toString();
        bookContent = etContent.getText().toString();
        bookPrice = etPrice.getText().toString();
        if (bookName.equals("")) {
            T.showShort(getBaseContext(), "请输入书名");
            return;
        }
        if (bookAuth.equals("")) {
            T.showShort(getBaseContext(), "请输入作者");
            return;
        }
        if (bookContent.equals("")) {
            T.showShort(getBaseContext(), "请输入介绍");
            return;
        }
        if (bookPrice.equals("")) {
            T.showShort(getBaseContext(), "请输入定价");
            return;
        }
        BookManage.changeBookInfo(dbread, bookModel.getBookID(), bookName, bookAuth, bookContent, bookPrice);
        T.showShort(getBaseContext(), "修改成功");
        finish();
    }
}
